{{extend '../_layouts/home.html'}}

{{block 'title'}}{{'多人博客 - 首页'}}{{/block}}

{{block 'body'}}
<section class="container">
  {{include '../_partials/settings-nav.html'}}
  <div class="col-md-5">
    <div class="subhead">
      <h2>修改密码</h2>
    </div>
    <hr>
    <form id="admin_form" action="/admin" method="post">
      <div class="form-group">
        <label for="exampleInputPassword1">当前密码</label>
        <input type="password" class="form-control" name='password' id="exampleInputPassword1" placeholder="请输入当前密码">
      </div>
      <div class="form-group">
        <label for="exampleInputPassword1">新的密码</label>
        <input type="password" class="form-control newpassword" name='newpassword' id="exampleInputPassword1" placeholder="请输入新密码">
      </div>
      <div class="form-group">
        <label for="exampleInputPassword1">确认密码</label>
        <input type="password" class="form-control newpassword1" name="newpassword1" id="exampleInputPassword1" placeholder="请再次输入新密码">
      </div>
      <button type="submit" class="btn btn-success">保存</button>
    </form>
    <div class="subhead">
      <form action="/delete" method="POST" id="delete_form">
        <h2>注销账号</h2>
        <hr>
      </div>
      <div>
        <p>一旦注销，不可找回，请谨慎操作</p>
        <button type="submit" class="btn btn-danger">删除</button>
      </form>
    </div>
    <hr>
  </div>
</section>
<script src="/node_modules/jquery/dist/jquery.js"></script>
    <script>
        $('#delete(form').on('submit',function(e){
          e.preventDefault()
          var formData = $(this).serialize()
          $.ajax({
            url:'/deleteUser',
            type:'post',
          })
        })
        $('#admin_form').on('submit', function(e) {
            //阻止默认提交方式
            e.preventDefault()
            //序列化表单数据
            var formData = $(this).serialize()
            $.ajax({
                url: '/admin',
                type: 'post',
                data: formData,
                dataType: 'json',
                success: function(data) {
                    var err_code = data.err_code
                    if(err_code === 1){
                      window.alert('旧密码错误！')
                    }else if(err_code === 2){
                      window.alert('输入的新密码不一致！')
                    }else{
                      alert('修改成功！请重新登录')
                      window.location.href = '/login'
                    }
                }
            })
        })
        
    </script>

{{/block}}
